/* -*-C-*-
*******************************************************************************
*
* File:         MailReader.m
* RCS:          $Header: /usr/local/lib/cvs/EnhanceMail/MailReader.m,v 1.1.1.16 1996/04/07 00:20:21 cedman Exp $
* Description:  
* Author:       Carl Edman
* Created:      Fri Oct 13 11:48:05 1995
* Modified:     Sat Apr  6 19:17:24 1996 (Carl Edman) cedman@capitalist.princeton.edu
* Language:     C
* Package:      N/A
* Status:       Experimental (Do Not Distribute)
*
* (C) Copyright 1995, but otherwise this file is perfect freeware.
*
*******************************************************************************
*/

#import "EnhanceMail.h"
#import "MailReader.h"
#import "MailBox.h"
#import "XFace.h"
#import "XImageURL.h"
#import "Preferences.h"

#define MENU_MESSAGE_FLAG NXLocalizedStringFromTableInBundle("Localizable", EnhanceBundle, "Message/Flag", NULL, Slashed menu path to flag command)
#define MENU_MESSAGE_STRIP NXLocalizedStringFromTableInBundle("Localizable", EnhanceBundle, "Message/Strip", NULL, Slashed menu path to strip command)
#define MENU_MAILBOX_INDEX NXLocalizedStringFromTableInBundle("Localizable", EnhanceBundle, "Mailbox/Index", NULL, Slashed menu path to create index command)
#define MENU_MAILBOX_UNINDEX NXLocalizedStringFromTableInBundle("Localizable", EnhanceBundle, "Mailbox/Delete index", NULL, Slashed menu path to remove index command)

static id index_menucell=nil;
static id indextag_image=nil;

@implementation EnhanceMailReader
+ finishLoading:(struct mach_header *)header
   {
   char path[MAXPATHLEN+1];
   
   if (EnhanceBundle==0) EnhanceBundle=[NXBundle bundleForClass:[self class]];
   [self poseAs:[self superclass]];
   [[NXApp mainMenu] addSlashItem:MENU_MESSAGE_FLAG action:@selector(toggleFlagged:) keyEquivalent:0];
#if 0
   [[NXApp mainMenu] addSlashItem:MENU_MESSAGE_STRIP action:@selector(strip:) keyEquivalent:0];
#endif
   index_menucell=[[NXApp mainMenu] addSlashItem:MENU_MAILBOX_INDEX action:@selector(toggleIndexing:) keyEquivalent:0];

   if ([EnhanceBundle getPath:path forResource:"indexTag" ofType:"tiff"])
      indextag_image=[[NXImage alloc] initFromFile:path];
   
   return self;
   }

- loadMessage:(int)fp16 newMsg:(BOOL)fp20 withAllHeaders:(BOOL)fp24
   {
   const char *header;
   id ret=[super loadMessage:fp16 newMsg:fp20 withAllHeaders:fp24];
   if (EnhanceShowXImageURL
       && (header=[mailMessage headerValueForKey:"X-Image-URL"]))
      [command setXImageURL:header];
   if (EnhanceShowXFace
       && (header=[mailMessage headerValueForKey:"X-Face"]))
      [command setXFace:header];
   return ret;
   }

- free
   {
   if (EnhanceAutoCompact && mailbox)
      {
      struct mboxstatus stat;
      [mailbox getMailboxStatus:&stat];
      if ((stat.delmsgno>0) && (![mailbox hasStore])) [self compact:self];
      }
   
   return [super free];
   }

- compact:sender
   {
   if (mailbox && [mailbox hasStore])
      [self toggleIndexing:sender];
   return [super compact:sender];
   }

- strip:sender
   {
#if 0
   id list=[matrix getSelectedCells:nil];
   id cell;
   int i;

   for(i=0;i<[list count];i++)
      {
      cell=[list objectAt:i];
      if (cell==nil) continue;
      /* XXX Strip every message of all NeXT/MIME/uuencoded attachments XXX */
      }
   
   list=[list free];
#endif
   return self;
   }

- (void)setFlagged:(BOOL)flag
   {
   id cell=[matrix selectedCell];
   if (cell==nil) return;
   [cell setFlagged:YES];
   if (mailbox==nil) return;
   [mailbox setState:'+' forMsg:[cell msgId] flush:NO];
   [self display];
   return;
   }

- toggleIndexing:sender
   {
   if (mailbox==nil) return nil;
   if ([mailbox hasStore])
      [mailbox deleteStore];
   else
      [mailbox createStore];

   [self display];
   [self windowDidBecomeMain:self];
   [self updateStatus];
   return self;
   }

- windowDidBecomeMain:sender
   {
   const char *c;

   if ((mailbox==nil)||(index_menucell==nil)) return self;
   
   c=[mailbox hasStore] ? MENU_MAILBOX_UNINDEX : MENU_MAILBOX_INDEX;
   if (c=rindex(c,'/')) [index_menucell setTitle:(c+1)];
   return self;
   }

- (void)updateStatus
   {
   const char *val;
   char buf[MAXPATHLEN+1];
   float size;
   
   [super updateStatus];

   if ((statusField==nil)
       ||(mailbox==nil)||((size=[mailbox storeSize])<0)) return;

   val=[statusField stringValue];

   if (strstr(val,"MB"))
      sprintf(buf,"%s \320 index (%.1fMB)",val,size/(1<<20));
   else if (strstr(val,"KB"))
      sprintf(buf,"%s \320 index (%.0fKB)",val,size/(1<<10));
   else
      sprintf(buf,"%s \320 index",val);
   
   [statusField setStringValue:buf];
   }

- drawSelf:(const NXRect *)rects :(int)rectCount
   {
   id ret=[super drawSelf:rects:rectCount];
   NXPoint p;
   NXSize s;
   if ((mailbox==nil)||(indextag_image==nil)) return nil;
   
   [indextag_image getSize:&s];
   p.x=0; p.y=s.height;
   if ([mailbox hasStore]) [indextag_image composite:NX_SOVER toPoint:&p];
   return ret;
   }
@end

@implementation CommandView(EnhanceMailReader)
- setXFace:(const char *)xface
   {
   NXSize size;
   
   if (face) return self;
   size.width=size.height=64;
   face=[[NXImage alloc] initXFace:xface size:&size];
   [self display];
   return self;
   }

- setXImageURL:(const char *)ximageurl
   {
   if (face) return self;
   face=[[NXImage alloc] initURL:ximageurl];
   [self display];
   return self;
   }
@end
